﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using NUnit.Framework;
using PS.Utils.FPRPC;
using PS.WssSync.Utils;
using PS.WssSync.WebPartService;

namespace PS.WssSync.Tests
{
    [TestFixture]
    public class WebPartPagesTests
    {
        private WebHelper webHelper;
        private FrontPageRPC fpRPC;

        [TestFixtureSetUp]
        public void Initailize()
        {
            webHelper = new WebHelper();
            fpRPC = new FrontPageRPC();
        }            

        [Test]
        public void RemoveAllWebParts()
        {
            WebUrl url = fpRPC.UrlToWebUrl("http://test01/loc-test/default1.aspx");
            webHelper.RemoveAllWebParts(url);
        }

        [Test]
        public void RegexTest()
        {
            const string str = "WebPartId=\"{12EEDCD1-0D7D-4C32-A3C3-11014883D848}\"";
            Regex rg = new Regex("WebPartId=\"{([0-9A-F-]*)}\"");
            MatchCollection matches = rg.Matches(str);
            string guid = matches[0].Groups[1].Value;
        }

        [Test]
        public void GetWebPartPageDocument()
        {
            string url = "http://saber-test/Pages/WeeklyEvents.aspx";
            WebUrl webUrl = fpRPC.UrlToWebUrl(url);
            string page = webHelper.GetDocument(webUrl);
        }

        [Test]
        public void GetMetaInfo()
        {
            string url = "http://saber-test/Pages/WeeklyEvents.aspx";
            WebUrl webUrl = fpRPC.UrlToWebUrl(url);
            DocumentInfo info = fpRPC.GetDocumentMetaInfo(webUrl);
        }

        [Test]
        public void GetDocument()
        {
            string url = "http://saber-test/Pages/WeeklyEvents.aspx";
            string res;
            WebUrl webUrl = fpRPC.UrlToWebUrl(url);
            using (MemoryStream stream = new MemoryStream())
            {
                DocumentInfo info = fpRPC.GetDocument(webUrl, stream);
                using (StreamReader sr = new StreamReader(stream))
                {
                    stream.Seek(0, SeekOrigin.Begin);
                    res = sr.ReadToEnd();
                }
            }
        }

        [Test]
        public void RemoveWebPartsFromText()
        {
            const string fileName = @"E:\Evgeniy\Projects\PS.Tools.WssSync\PS.WssSync\bin\Debug\MyPage.aspx";
            string source = File.Exists(fileName) ? File.ReadAllText(fileName, Encoding.Default) : string.Empty;
            
            if(!string.IsNullOrEmpty(source))
            {
                ICollection<string> removedWebParts;
                string docWithoutWebParts = webHelper.RemoveWebPartsFromText(source, out removedWebParts);
                File.WriteAllText(fileName, docWithoutWebParts, Encoding.Default);
            }
        }

        [Test]
        public void AddWebPart()
        {
            string webPartXml =
                File.ReadAllText(@"E:\Evgeniy\Projects\PS.Tools.WssSync\PS.WssSync\bin\Debug\GetWebPart.aspx", Encoding.Default);            
            webHelper.AddWebPart("http://saber-test/Pages/MyPage.aspx", webPartXml, Storage.Personal);
        }
        
        [Test]
        public void GetWebPart()
        {
            string result = webHelper.GetWebpart2("http://saber-test/Pages/MyPage.aspx", new Guid("A145A36B-5B4C-4623-B5D7-EAADB1FF92E7"), Storage.Shared, SPWebServiceBehavior.Version3);
            File.WriteAllText(@"E:\Evgeniy\Projects\PS.Tools.WssSync\PS.WssSync\bin\Debug\GetWebPart.aspx", result, Encoding.Default);
        }
    }
}